package com.cjbdi.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.cjbdi.entity.GongGao;
import com.cjbdi.entity.UserInfo;

@Repository
public interface GonggaoRepository extends JpaRepository<GongGao, Long> {
	
	@Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "delete from gonggao where id=:id",nativeQuery = true)//原生SQL
    Integer delete(@Param("id")int id);
    
    @Query(value = "select * from gonggao where case when :title!=''then title like '%'||:title||'%' else 1=1 end and case when :fymc!='' then fymc like '%'||:fymc||'%' else 1=1 end and case when :tag!=''then tag=:tag else 1=1 end" , nativeQuery = true)
    Page<GongGao> getPageByParam(@Param("title") String title,@Param("fymc") String fymc,@Param("tag") String tag,Pageable pageable);
    
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update gonggao set tag=:tag where title=:title",nativeQuery = true)//原生SQL
    Integer updateTag(@Param("title")String title,@Param("tag")String tag);
    
    @Query(value = "select * from gonggao as a where a.title = :title", nativeQuery = true)
    List<GongGao> findByTitle(@Param("title")String title);
    
    @Query(value = "select * from gonggao as a where a.id = :id", nativeQuery = true)
    List<GongGao> findById(@Param("id")int id);
    
    @Query(value = "select * from gonggao where tag='已发送'  and case when :fybm!=''then fybm like '%'||:fybm||'%' else 1=1 end order by sent_time desc", nativeQuery = true)
    List<GongGao> getLastGonggao(@Param("fybm")String fybm);
    
}
